package cn.stylefeng.guns.modular.business.frequency.ordertransportrelation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.ordertransportrelation.entity.OrderTransportRelation;
import cn.stylefeng.guns.modular.business.frequency.ordertransportrelation.mapper.OrderTransportRelationMapper;
import cn.stylefeng.guns.modular.business.frequency.ordertransportrelation.model.param.OrderTransportRelationParam;
import cn.stylefeng.guns.modular.business.frequency.ordertransportrelation.service.OrderTransportRelationService;
import cn.stylefeng.guns.modular.business.frequency.ordertransportrelation.enums.OrderTransportRelationExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * tt_order_transport_relation 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:16
 */
@Service
public class OrderTransportRelationServiceImpl extends ServiceImpl<OrderTransportRelationMapper, OrderTransportRelation> implements OrderTransportRelationService {

    @Override
    public PageResult<OrderTransportRelation> page(OrderTransportRelationParam orderTransportRelationParam) {

        // 构造条件
        LambdaQueryWrapper<OrderTransportRelation> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<OrderTransportRelation> list(OrderTransportRelationParam orderTransportRelationParam) {

        // 构造条件
        LambdaQueryWrapper<OrderTransportRelation> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(OrderTransportRelationParam orderTransportRelationParam) {

        // 将dto转为实体
        OrderTransportRelation orderTransportRelation = new OrderTransportRelation();
        BeanUtil.copyProperties(orderTransportRelationParam, orderTransportRelation);

        this.save(orderTransportRelation);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(OrderTransportRelationParam orderTransportRelationParam) {
        this.removeById(orderTransportRelationParam.getId());
    }

    @Override
    public void edit(OrderTransportRelationParam orderTransportRelationParam) {

        // 根据id查询实体
        OrderTransportRelation orderTransportRelation = this.queryOrderTransportRelation(orderTransportRelationParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(orderTransportRelationParam, orderTransportRelation);

        this.updateById(orderTransportRelation);
    }

    @Override
    public OrderTransportRelation detail(OrderTransportRelationParam orderTransportRelationParam) {
        return this.queryOrderTransportRelation(orderTransportRelationParam);
    }



    /**
     * 获取tt_order_transport_relation
     *
     * @author yujie
     * @since 2023/05/09 17:16
     */
    private OrderTransportRelation queryOrderTransportRelation(OrderTransportRelationParam orderTransportRelationParam) {
        OrderTransportRelation orderTransportRelation = this.getById(orderTransportRelationParam.getId());
        if (ObjectUtil.isEmpty(orderTransportRelation)) {
            throw new ServiceException(OrderTransportRelationExceptionEnum.NOT_EXISTED);
        }
        return orderTransportRelation;
    }
}